import QtQuick 2.0
import QtQuick.Controls 2.2

Item
{
    id: _toastRoot

    property int layoutY: 75
    property int maxWidth: 300
    property var screenLayout: null

    property string const_success: "success";
    property string const_info:    "info";
    property string const_warning: "warning";
    property string const_error:   "error";

    function showSuccess(text,duration,moremsg){
        _toastRoot.create(_toastRoot.const_success,text,duration,moremsg ? moremsg : "");
    }

    function showInfo(text,duration,moremsg){
        _toastRoot.create(_toastRoot.const_info,text,duration,moremsg ? moremsg : "");
    }

    function showWarning(text,duration,moremsg){
        _toastRoot.create(_toastRoot.const_warning,text,duration,moremsg ? moremsg : "");
    }

    function showError(text,duration,moremsg){
        _toastRoot.create(_toastRoot.const_error,text,duration,moremsg ? moremsg : "");
    }


    function create(type,text,duration,moremsg)
    {
        if(null !== screenLayout)
        {
            var last = screenLayout.getLastloader()
            if(last.type === type && last.text === text && moremsg === last.moremsg)
            {
                last.restart()
                return
            }
        }

        initScreenLayout();
        contentComponent.createObject(screenLayout,{
                                          type:type,
                                          text:text,
                                          duration:duration,
                                          moremsg:moremsg,
                                      })
    }

    function initScreenLayout()
    {
        if(screenLayout == null)
        {
            screenLayout = screenlayoutComponent.createObject(mainWindow)
            screenLayout.y = _toastRoot.layoutY
            screenLayout.z = 100000
        }
    }

    // 多个toast布局组件
    Component
    {
        id: screenlayoutComponent
        Column
        {
            spacing: 10
            width: parent.width;
            move: Transition
            {
                NumberAnimation { properties: "y"; easing.type: Easing.OutBack; duration: 300 }
            }

            onChildrenChanged:
            {
                if(children.length === 0)
                {
                    destroy()
                }
            }

            function getLastloader()
            {
                if(children.length > 0)
                {
                    return children[children.length - 1]
                }
                return null
            }
        }
    }

    Component
    {
        id: contentComponent
        Item
        {
            id:content;
            property int    duration: 2000
            property var    itemcomponent
            property string type;
            property string text;
            property string moremsg;

            width:  parent.width
            height: loader.height

            function close()
            {
                content.destroy()
            }

            function restart()
            {
                delayTimer.restart();
            }

            Timer
            {
                id: delayTimer
                interval: duration; running: true; repeat: true
                onTriggered: content.close();
            }

            Loader
            {
                id: loader;
                x: (parent.width - width) / 2
                property var _super: content;

                scale: item ? 1 : 0;
                asynchronous: true

                Behavior on scale
                {
                    NumberAnimation {
                        easing.type: Easing.OutBack;
                        duration: 100
                    }
                }

                sourceComponent:itemcomponent ? itemcomponent : _toastRoot.toastDelegate
            }

        }
    }

    // 单个toast内容组件
    property Component toastDelegate:  Rectangle
    {
        id:rect;
        width:  rowlayout.width  + (_super.moremsg ? 25 : 80);
        height: rowlayout.height + 20;
        color: {
            switch(_super.type){
                case _toastRoot.const_success: return "#F0F9EB";
                case _toastRoot.const_warning: return "#FDF6ED";
                case _toastRoot.const_info:    return "#EDF2FC";
                case _toastRoot.const_error:   return "#FEF0F0";
            }
            return "#FFFFFF"
        }
        radius: 4
        border.width: 1;
        border.color: Qt.lighter(ticon.color,1.2)

        Row {
            id: rowlayout
            x: 20
            y: (parent.height - height) / 2
            spacing: 10
            Image {
                id:ticon
                anchors.verticalCenter: parent.verticalCenter;
                property color color: {
                    switch("const_error"){
                    case "const_success": return "#6AC044";
                    case "const_warning": return "#E4A147";
                    case "const_info":    return "#909399";
                    case "const_error":   return "#F36D6F";
                    }
                    return "#FFFFFF"
                }
                source:{
                    switch(_super.type){
                        case _toastRoot.const_success: return "qrc:/resource/svg/success.svg";
                        case _toastRoot.const_warning: return "qrc:/resource/svg/warning.svg";
                        case _toastRoot.const_info:    return "qrc:/resource/svg/info.svg";
                        case _toastRoot.const_error:   return "qrc:/resource/svg/error.svg";
                    }
                    return "#FFFFFF"
                }

                width:  more.visible ? 40 : 22;
                height: more.visible ? 40 : 22;
            }

            Column {
                spacing: 5;
                Label{
                    font.bold: more.visible
                    font.pixelSize: 20;
                    text: _super.text
                    color: ticon.color;
                }

                Label{
                    id: more
                    color:    ticon.color;
                    text:    _super.moremsg;
                    visible: _super.moremsg;
                    wrapMode : Text.WordWrap

                    onContentWidthChanged: {
                        width = contentWidth < _toastRoot.maxWidth - 100 ? 220 : _toastRoot.maxWidth
                    }
                }
            }
        }

        Button{
            icon.width:  12
            icon.height: 12
            width: 20
            height: 20
            y: 4
            x: parent.width - width-2
            //icon.source: "qrc:/resource/svg/close-px.svg"
            //icon.color:"#ADADAD";
            Image {
                source: "qrc:/resource/svg/close-px.svg"
                smooth: true
                fillMode: Image.PreserveAspectFit  // ensure it fits
                anchors.fill: parent
            }
            onClicked: _super.close();
        }

    }
}
